package com.atcumt.Huawei;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 迷宫问题
 * <p>
 * 输入：
 * 5 5
 * 0 1 0 0 0
 * 0 1 1 1 0
 * 0 0 0 0 0
 * 0 1 1 1 0
 * 0 0 0 1 0
 * <p>
 * 输出：
 * (0,0)
 * (1,0)
 * (2,0)
 * (2,1)
 * (2,2)
 * (2,3)
 * (2,4)
 * (3,4)
 * (4,4)
 *
 */


// 简单的位置类

public class HJ3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] nums = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                nums[i][j] = sc.nextInt();
            }
        }

        List<Pos> path = new ArrayList<>();
        dfs(nums, 0, 0, path);
        for (Pos p : path) {
            System.out.println("(" + p.x + "," + p.y + ")");
        }
    }

    private static boolean dfs(int[][] nums, int x, int y, List<Pos> path) {
        path.add(new Pos(x, y)); // 添加路径，标记已走过
        nums[x][y] = 1;

        // 终止条件
        if (x == nums.length - 1 && y == nums[0].length - 1) {
            return true;
        }

        // 做出选择：下、右、上、左
        if (x + 1 < nums.length && nums[x + 1][y] == 0) {
            if (dfs(nums, x + 1, y, path)) {
                return true;
            }
        }

        if (y + 1 < nums[0].length && nums[x][y + 1] == 0) {
            if (dfs(nums, x, y + 1, path)) {
                return true;
            }
        }

        if (x - 1 > -1 && nums[x - 1][y] == 0) {
            if (dfs(nums, x - 1, y, path)) {
                return true;
            }
        }

        if (y - 1 > -1 && nums[x][y - 1] == 0) {
            if (dfs(nums, x, y - 1, path)) {
                return true;
            }
        }

        // 撤销选择
        path.remove(path.size() - 1);
        nums[x][y] = 0;
        return false;
    }
}

class Pos {
    int x;
    int y;

    public Pos(int x, int y) {
        this.x = x;
        this.y = y;
    }
}